﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;
using ComLib.ComType;
using System.IO;
using System.Collections;
using ComLib.ConfigService;
using System.Runtime.InteropServices;

namespace CardPhotoQCServer.QC
{
    public class QCHandler
    {
        private string _error = "";
        public string ErrorInfo
        {
            get { return _error; }
        }

        public readonly int HandleType = 0;

        protected QCMode _mode = QCMode.Mandatory;
        public QCMode CheckMode
        {
            get { return _mode; }
        }

        private bool _bInitQCLib = false;
        public bool HasInited
        {
            get { return _bInitQCLib; }
        }

        public QCHandler(int ty)
        {
            HandleType = ty;
        }

        protected virtual int InitSDK()
        {
            return 0;
        }

        protected virtual int UninitSDK()
        {
            return 0;
        }

        public virtual int CardPhotoQC(String strSrcFile, String okPath, String failPath, int paramInt)
        {
            return 0;
        }

        public bool Init()
        {
            _error = "";
            int nRes = InitSDK();
            if (nRes == 0)
            {
                _bInitQCLib = true;
                Log.WriteAsync("一所质检类库初始化成功.");
                return true;
            }
            else
            {
                _error = string.Format("一所质检类库初始化失败: 错误代码{0}", nRes);
                Log.WriteAsync(_error);
                return false;
            }
        }

        public bool Uninit()
        {
            _error = "";
            int nRes = UninitSDK(); 
            if (nRes == 0)
            {
                _bInitQCLib = true;
                Log.WriteAsync("一所质检类库卸载成功.");
                return true;
            }
            else
            {
                _error = string.Format("一所质检类库卸载失败: 错误代码{0}", nRes);
                Log.WriteAsync(_error);
                return false;
            }
        }
    }

    /// <summary>
    /// 一所质检，硬件版本，强制模式
    /// </summary>
    public class PhotoQCHard_Mdty : QCHandler
    {
        public const string strDllPath = "PhotoQC\\Hard\\CardPhotoQC_Mandatory.dll";
        [DllImport(strDllPath)]
        public static extern int InitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int UninitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Mandatory(String paramString1, String paramString2, String paramString3, int paramInt);

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Mandatory(byte[] paramArrayOfByte1, int paramInt1, byte[] paramArrayOfByte2, int paramInt2);

        public PhotoQCHard_Mdty()
            : base(0)
        {
            _mode = QCMode.Mandatory;
        }

        protected override int InitSDK()
        {
            Log.WriteAsync("PhotoQCHard_Mdty InitSDK.");
            return InitFaceQSDK();
        }

        protected override int UninitSDK()
        {
            return UninitFaceQSDK();
        }

        public override int CardPhotoQC(string strSrcFile, string okPath, string failPath, int paramInt)
        {
            return CardPhotoQC_Mandatory(strSrcFile, okPath, failPath, paramInt);
        }
    }

    public class PhotoQCHard_Otal : QCHandler
    {
        public const string strDllPath = "PhotoQC\\Hard\\CardPhotoQC_Optional.dll";
        [DllImport(strDllPath)]
        public static extern int InitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int UninitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Optional(String paramString1, String paramString2, String paramString3, int paramInt);

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Optional(byte[] paramArrayOfByte1, int paramInt1, byte[] paramArrayOfByte2, int paramInt2);

        public PhotoQCHard_Otal()
            : base(1)
        {
            _mode = QCMode.Optional;
        }

        protected override int InitSDK()
        {
            Log.WriteAsync("PhotoQCHard_Otal InitSDK.");
            return InitFaceQSDK();
        }

        protected override int UninitSDK()
        {
            return UninitFaceQSDK();
        }

        public override int CardPhotoQC(string strSrcFile, string okPath, string failPath, int paramInt)
        {
            return CardPhotoQC_Optional(strSrcFile, okPath, failPath, paramInt);
        }
    }

    /// <summary>
    /// 一所质检
    /// </summary>
    public class PhotoQCSoft_Mdty : QCHandler
    {
        public const string strDllPath = "PhotoQC\\Soft\\CardPhotoQC_Mandatory.dll";
        [DllImport(strDllPath)]
        public static extern int InitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int UninitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Mandatory(String paramString1, String paramString2, String paramString3, int paramInt);

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Mandatory(byte[] paramArrayOfByte1, int paramInt1, byte[] paramArrayOfByte2, int paramInt2);

        public PhotoQCSoft_Mdty()
            : base(2)
        {
            _mode = QCMode.Mandatory;
        }

        protected override int InitSDK()
        {
            return InitFaceQSDK();
        }

        protected override int UninitSDK()
        {
            return UninitFaceQSDK();
        }

        public override int CardPhotoQC(string strSrcFile, string okPath, string failPath, int paramInt)
        {
            return CardPhotoQC_Mandatory(strSrcFile, okPath, failPath, paramInt);
        }
    }

    public class PhotoQCSoft_Otal : QCHandler
    {
        public const string strDllPath = "PhotoQC\\Soft\\CardPhotoQC_Optional.dll";
        [DllImport(strDllPath)]
        public static extern int InitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int UninitFaceQSDK();

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Optional(String paramString1, String paramString2, String paramString3, int paramInt);

        [DllImport(strDllPath)]
        public static extern int CardPhotoQC_Optional(byte[] paramArrayOfByte1, int paramInt1, byte[] paramArrayOfByte2, int paramInt2);

        public PhotoQCSoft_Otal()
            : base(3)
        {
            _mode = QCMode.Optional;
        }

        protected override int InitSDK()
        {
            return InitFaceQSDK();
        }

        protected override int UninitSDK()
        {
            return UninitFaceQSDK();
        }

        public override int CardPhotoQC(string strSrcFile, string okPath, string failPath, int paramInt)
        {
            return CardPhotoQC_Optional(strSrcFile, okPath, failPath, paramInt);
        }
    }
}
